package demo.preference.com.bluetoothdemo;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;

import cn.hlife.com.bluetooth20_connect.lib.BluetoothService;
import cn.hlife.com.bluetooth20_connect.util.LogUtils;

public class Bluetooth2 extends AppCompatActivity {
    private static final int REQUEST_CONNECT_DEVICE = 1;  //获取设备消息
    private static final int REQUEST_ENABLE_BT = 2;
    BluetoothService mService = null;
    BluetoothDevice con_dev = null;
    TextView  showData;
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case BluetoothService.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:   //已连接
                            Toast.makeText(getApplicationContext(), "连接成功",
                                    Toast.LENGTH_SHORT).show();

                            break;
                        case BluetoothService.STATE_CONNECTING:  //正在连接
                            LogUtils.e("蓝牙调试 正在连接.....");
                            break;
                        case BluetoothService.STATE_LISTEN:     //监听连接的到来
                        case BluetoothService.STATE_NONE:
                            LogUtils.e("蓝牙调试 等待连接.....");
                            break;
                    }
                    break;
                case BluetoothService.MESSAGE_CONNECTION_LOST:    //蓝牙已断开连接
                    Toast.makeText(getApplicationContext(), "Device connection was lost",
                            Toast.LENGTH_SHORT).show();

                    break;
                case BluetoothService.MESSAGE_UNABLE_CONNECT:     //无法连接设备
                    Toast.makeText(getApplicationContext(), "连接失败请重试",
                            Toast.LENGTH_SHORT).show();
                    break;
                case BluetoothService.MESSAGE_NOTIFY:     //无法连接设备
                    byte []  bytes =(byte[]) msg.obj;
                    showData.setText(String.valueOf(bytes));
                    break;
            }
        }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth2);
        showData=(TextView) findViewById(R.id.textView);
        findViewById(R.id.search_button).setOnClickListener(viewClick);
        findViewById(R.id.button2).setOnClickListener(viewClick);
        mService = new BluetoothService(this, mHandler);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mService.isBTopen() == false) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
    }

    public View.OnClickListener  viewClick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.search_button:
                    Intent serverIntent = new Intent(Bluetooth2.this, DeviceListActivity.class);      //运行另外一个类的活动
                    startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                    break;
                case R.id.button2:
                    String  name="我是好人\n";
                    try {
                        mService.write(name.getBytes("GBK"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    break;
            }

        }
    };
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_ENABLE_BT:      //请求打开蓝牙
                if (resultCode == Activity.RESULT_OK) {   //蓝牙已经打开
                    Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
                } else {                 //用户不允许打开蓝牙
                    finish();
                }
                break;
            case REQUEST_CONNECT_DEVICE:     //请求连接某一蓝牙设备
                if (resultCode == Activity.RESULT_OK) {   //已点击搜索列表中的某个设备项
                    String address = data.getExtras()
                            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);  //获取列表项中设备的mac地址
                    con_dev = mService.getDevByMac(address);
                    if (con_dev != null) {
                        mService.connect(con_dev);
                    } else {
                        Toast.makeText(getApplicationContext(), "没有找到该设备",
                                Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }
}
